var Cyfy = {
    pathPrefix : "",
    language : "en",
    query : "",
    multiLanguage : false,
    messageTimer : false,
    msgEffect: 'drop',
    msgWait: 400,
    msgEffectLength: 300,
    msgLength: 5000,
    
  redirectTo : function(link, time){
        if(time){
            return setTimeout("Cyfy.redirectTo('"+link+"');", time);
        }
    return window.open(this.getPath(link), "_self");
  },
  
  redirectToOutside : function(link){
    return window.open(link, "_self");
  },

  
  refreshPage : function(){
	location.reload(true)
  },
  
  ajax : function(module, ajax, place, send, callback, disableAnimation) {
        if(place != "" && !disableAnimation){
            $(place).css("overflow","auto");
            $(place).css("margin","0");
            $(place).css("padding","0");
            $(place).fadeTo(150, 0.0);
            var originalHeight = $(place).outerHeight();
            
            // loading image:
            var offset = $(place).offset();
            $("body").append("<div class=\"ajaxLoader\"></div>");
            $(".ajaxLoader").css({
                width:$(place).innerWidth(),
                minHeight:$(place).outerHeight(),
                position:"absolute",
                top: (offset.top - 1) + 'px',   
                zIndex: '13',
                left: (offset.left - 1) + 'px',
            });
        }
        $.ajax({
            type: "POST",
            url: this.getSourcePath('ajax.php'),
            data: "module=" + module + "&ajax=" + ajax + "&url=" + location.pathname + "&cyfy-language=" + this.language + "&" + send,
            async: true,
            dataType: "json",
            success: function(data){
				
                if(data.messages){
                    Cyfy.setMessages(data.messages);
                }
                if(data.text && place != ""){
                    $(place).html(data.text);
					
					if(!disableAnimation){
						$(place).css("height","100%");
						var newHeight = $(place).outerHeight({margin: false});
						$(place).css("overflow","hidden");
						$(place).css("height",originalHeight);
                        
						$(place).animate({'height': newHeight},200, function(){
                            $(place).fadeTo(200, 1);
							if(callback) eval(callback);
							$(place).css("height","");
							$(place).css("overflow","auto");
                            $(".ajaxLoader").remove();
						});
                        
					}else{
						if(callback) eval(callback);
					}   
                }else{
                    if(callback) eval(callback);
                }
            },
            error: function(jqXHR, textStatus, errorThrown)
            {
                Cyfy.setMessage(errorThrown);
                $(place).fadeTo(200, 1);
                $(".ajaxLoader").remove();
            }
        });
    },

    getSourcePath : function(url)
    {
        return this.pathPrefix + url;
    },
    
    getPath : function(url)
    {
        if(this.multiLanguage)
        {
            return this.getSourcePath(this.language + "/" + url);
        }
        
        return  this.getSourcePath(url);
    },
    
    setMessage : function(text, type)
    {
        if(type == ""){
            type = status;
        }
        this.setMessages("<div class=\"message "+type+"\">" + text + "</div>");    
    },
    
    setMessages : function(text)
    {
        // add to cache
        $("#cyfy-messages-cache").append(text);
        
        // no multiple timers
        clearInterval(Cyfy.messageTimer);
        
        // get messages one by one from cache
        this.messageTimer = setInterval("Cyfy.showMessageFromCache();", Cyfy.msgWait);
        Cyfy.showMessageFromCache();
        
    },
    
    showMessageFromCache : function()
    {
        // if empty - stop timer
        if($("#cyfy-messages-cache").text() == '') clearInterval(Cyfy.messageTimer);
        
        // unique message id
        var newDate = new Date;
        var unique = newDate.getTime();        
        
        // get
        $("#cyfy-messages-cache .message").first().hide().appendTo("#cyfy-messages").wrap("<div class='message-wrapper message-"+unique+"' />").show(Cyfy.msgEffect, { direction: "up" }, Cyfy.msgEffectLength).click(function()
        {
            $(this).parent().remove();
        });
        
        // set message timeout
        setTimeout("$('.message-"+unique+"').hide('"+ Cyfy.msgEffect  +"', { direction: \"up\" }, "+ Cyfy.msgEffectLength+")", Cyfy.msgLength);
    },
    
    initMessages : function(){
     //  $("#cyfy-messages .message")
    },
    
    clearMessages : function(){
        $("#cyfy-messages").html("");
    }

}
    
    

Array.prototype.moveUp = function(k){
  if(k > 0 && k < this.length){
    var tmp = this.splice(k,1);
    this.splice(k - 1,0,tmp[0]);
  }
}

Array.prototype.moveDown = function(k){
  if(k >= 0 && k < (this.length - 1)){
    var tmp = this.splice(k,1);
    this.splice(k + 1,0,tmp[0]);
  }
}

Array.prototype.removeAt = function(k){
  if(k >= 0 && k < this.length){
    this.splice(k, 1);
  }
}

Array.prototype.inArray = function(str){
  for(var i=0; i<this.length; i++)
    if (this[i] == str) return true;
  return false;
}



$(document).ready(function () {
	Cyfy.setMessages("");
    $("#cyfy-messages-cache").hide();
});

